package com.itheima.text;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns ="/loginStudentServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String passwd = req.getParameter("passwd");

        if (username.equals("itheima") && passwd.equals("123456")) {
            resp.sendRedirect("/day3/index.html");
        }else{
            resp.setContentType("com.itheima.text/html;charset=utf-8");
            PrintWriter pw = resp.getWriter();
            pw.write("用户名或密码错误，3秒钟跳转到登陆页面。。。");
            resp.setHeader("Refresh","3;URL=/day3/login.html");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
